package com.log4jviewer.ui.preferences.additional;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColorCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.graphics.RGB;

/**
 * Editor for choosing log foreground color from the log display table preferences.
 * 
 * @author <a href="mailto:rd.ryly@gmail.com">Ruslan Diachenko</a>
 */
public class LogForegroundCellEditor extends EditingSupport {

    private final TableViewer tableViewer;

    public LogForegroundCellEditor(final TableViewer tableViewer) {
        super(tableViewer);
        this.tableViewer = tableViewer;
    }

    @Override
    protected CellEditor getCellEditor(final Object element) {
        return new ColorCellEditor(tableViewer.getTable());
    }

    @Override
    protected boolean canEdit(final Object element) {
        return true;
    }

    @Override
    protected Object getValue(final Object element) {
        return ((LogLevelDisplayModel) element).getForeground();
    }

    @Override
    protected void setValue(final Object element, final Object value) {
        LogLevelDisplayModel levelDisplayModel = (LogLevelDisplayModel) element;
        levelDisplayModel.setForeground((RGB) value);
        tableViewer.update(element, null);
    }
}
